package sort;

public class BubbleSort implements Sort{
    @Override
    public void sort(int[] arr, boolean asc) {
        int len = arr.length;
        boolean flag = true;
        for (int i = len - 1; i >= 0 && flag; --i) {
            flag = false;
            for (int j = 0; j < i; ++j) {
                // 如果有元素进行交换则说明当前序列并非有序
                if ((asc && arr[j] > arr[j + 1]) || (!asc && arr[j] < arr[j + 1])) {
                    int tmp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = tmp;
                    flag = true;
                }
            }
        }
    }
}
